<script type="text/html" template lay-done="layui.data.temporaryPastdone(d);">
    <div class='temporary-pastHtml-content'>
        <div class="layui-row">
            <div class="layui-col-md6">
                <div class="layui-card-header">
                    选择单位：
                </div>
                <div class="ztreeContainer">
                    <ul id='temporary-pastHtml-ztree-department' class="ztree"></ul>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-card-header">
                    选择群组：
                </div>
                <div class="ztreeContainer">
                    <ul id='temporary-pastHtml-ztree-group' class="ztree"></ul>
                </div>
            </div>
            <div class="layui-col-md12">
                <div class="department-check">
                    <div class="check-value-box">
                        {{#
                            if(d.params.exitDep) {
                                var nameStr = [];
                                layui.each(d.params.exitDep, function(index,item) {
                                    nameStr.push(item.writeusername);
                                })
                        }}
                            {{nameStr.join()}}
                        {{#
                            }
                        }}
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-row" style="text-align: center;margin-top: 15px;">
            <textarea class="data-dep-container" style="display: none;" name="" id="" cols="30" rows="10">
                {{#
                    if(d.params.exitDep) {
                }}
                        {{JSON.stringify(d.params.exitDep)}}
                {{#
                    }else {
                }}
                        []
                {{#
                    }
                }}
            </textarea>
            <button type="button" class="layui-btn T-confirm" data-groupstr="" data-depstr="">确认</button>
        </div>
    </div>
</script>
<style>
    .temporary-pastHtml-content {
        padding: 0px 10px;
    }
    .temporary-pastHtml-content .layui-btn{
        background: #2669B1;
        width: 20%;
        border-radius: 6px;
        font-size: 15px;
        letter-spacing: 2px;
        -ms-behavior: url(../../dist/style/res/PIE.htc);
	    behavior: url(../../dist/style/res/PIE.htc);
    }
    .temporary-pastHtml-content .ztreeContainer {
        height: 400px;
        overflow-y: auto;
    }
    
    .temporary-pastHtml-content .department-check,
    .temporary-pastHtml-content .group-check {
        height: 70px;
        border: 1px solid #ccc;
        margin: 15px 0px 0px 13px;
        padding: 2px 2px;
    }

    .temporary-pastHtml-content .check-value-box {
        width: 100%;
        height: 100%;
        overflow-y: auto;
    }
</style>
<script> 
    layui.data.temporaryPastdone = function (d) {
      
        layui.use(['admin', 'tools'], function () {
            var admin = layui.admin,
                Tools = layui.tools;

            var allChecked = d.params.exitDep ?  d.params.exitDep : [];
            var zTreeObjDep, zTreeObjGroup;
            if(Tools.isIE()){ 
                  $('.temporary-pastHtml-content .layui-btn').css({
                      "behavior":"none",
                     " -ms-behavior": "url(../../dist/style/res/PIE.htc)"
                  })
            }
            //单位树节点配置
            var departmentSetting = {
                check: {
                    enable: true,
                    chkStyle: "checkbox",
                    chkboxType: {
                        "Y": "",
                        "N": ""
                    }
                },
                data: {
                    key: {
                        name: "dept_name",
                    },
                    simpleData: {
                        idKey: "id",
                        pIdKey: "pId",
                        rootPId: 0
                    }
                },
                async: {
                    enable: true,
                    type: 'get',
                    url: "myapi/DC/api/rest/web/custom/GetDeptInfo?jsonId="+layui.data('layuiAdmin').access_token+"&access_token="+layui.data('layuiAdmin').access_token,
                    dataFilter: ajaxDataFilter,
                    autoParam: ["id"]
                },
                callback: {
                    onCheck: departmentCheck
                }
            };

            //群组树节点配置
            var groupSetting = {
                check: {
                    enable: true,
                    autoCheckTrigger: true,
                    chkStyle: "checkbox",
                    chkboxType: {
                        "Y": "s",
                        "N": "ps"
                    }
                },
                data: {
                    key: {
                        name: "groupname",
                        children: 'chlid',
                    },
                    simpleData: {
                        idKey: "id",
                        pIdKey: "pId",
                        rootPId: 0
                    }
                },
                callback: {
                    onCheck: groupCheck
                }
            };

            //过滤单位节点返回的数据
            function ajaxDataFilter(treeId, parentNode, responseData) {
                var nodesData = [];
                if (responseData.retcode === '0' && responseData.data && responseData.data.length >0) {
                    $.each(responseData.data, function (index, item) {
                        $.each(allChecked, function(i,t) {
                            if(t.dept_id === item.id) {
                                item.checked=true;
                            }
                        })
                        item.isParent = true;
                        nodesData.push(item);
                    })
                }
                return nodesData;
            };

            //过滤群组节点返回数据
            function ajaxGroupFilter(treeId, parentNode, responseData) {
                var nodesData = [];
                if (responseData.retcode === '0' && responseData.data && responseData.data.data.length >0) {
                    var allArr = exGgrp;
                    $.each(responseData.data.data, function (index, item) {
                        $.each(allArr, function(i,t) {
                            if(t.id === item.id) {
                                item.checked=true;
                            }
                        })
                        item.groupname = item.dept_name;
                        item.group_id = item.id;
                        item.isParent = true;
                        nodesData.push(item);
                    })
                }
                return nodesData;
            }
            //单位选择
            function departmentCheck(event, treeId, treeNode) {
                var grpNode = zTreeObjGroup.getNodesByParam('id',treeNode.id);
                if(grpNode && grpNode.length > 0 && ((grpNode[0].level*1) !== 0)) {
                    zTreeObjGroup.checkNode(grpNode[0], treeNode.checked, false);
                }
                checkEvent(treeNode);
            }

            //群组选择
            function groupCheck(event, treeId, treeNode) {
                if((treeNode.level*1) !== 0) {
                    var depNode = zTreeObjDep.getNodesByParam('id', treeNode.id);
                    if(depNode && depNode.length > 0 ) {
                        zTreeObjDep.checkNode(depNode[0], treeNode.checked, false);
                    }
                    checkEvent(treeNode);
                }
            }

            //节点选择事件
            function checkEvent(treeNode) {
                if(!treeNode.checked) {
                    for(var i=allChecked.length-1;i>=0;i--) {
                        if (allChecked[i].dept_id === treeNode.id) {
                            allChecked.splice(i,1);
                        }
                    }
                }else {
                    allChecked.push({
                        dept_name: treeNode.dept_name,
                        dept_id: treeNode.id,
                        dataid: treeNode.id,
                        dept_code: treeNode.dept_code,
                        writeusername: treeNode.dept_name
                    });
                }
                allChecked = Tools.uniqueArr(allChecked, 'dept_id');
                var $container = $('.temporary-pastHtml-content');
                var nameStr = [];
                $.each(allChecked, function (index, item) {
                    nameStr.push(item.writeusername);
                });
                $container.find('.check-value-box').text(nameStr.join());
                $container.find('.data-dep-container').val(JSON.stringify(allChecked));
            };

            //初始化单位树
            admin.req({ 
                url: 'myapi/DC/api/rest/web/custom/GetDeptInfo', 
                data: {
                    jsonId: layui.data('layuiAdmin').access_token
                },
                success: function (res) {
                    var dataArr = [];
                    $.each(res.data, function (index, item) {
                        $.each(allChecked, function(i,t) {
                            if(t.dept_id === item.id) {
                                item.checked=true;
                            }
                        })
                        item.isParent = true;
                        dataArr.push(item);
                    })
                    zTreeObjDep = $.fn.zTree.init($("#temporary-pastHtml-ztree-department"),
                        departmentSetting, dataArr);
                }
            });

            //初始化群组树
            admin.req({
                url: 'myapi/DC/api/rest/web/custom/GroupTreeBytaskid',
                data: {
                    taskid: d.params.taskid
                },
                success: function (res) {
                    var dataArr = [];
                    $.each(res.data.data, function (index, item) {
                        if(item.chlid.length > 0) {
                            $.each(allChecked, function(i,t) {
                                $.each(item.chlid, function(ci,cl) {
                                    if(t.dept_id === cl.id) {
                                        cl.checked=true;
                                    }
                                })
                            })
                        }
                        item.isParent = true;
                        dataArr.push(item);
                    })
                    zTreeObjGroup = $.fn.zTree.init($("#temporary-pastHtml-ztree-group"),
                    groupSetting, dataArr);
                }
            });
        })
    }
</script>